/**
 * File Name	: UserService.java
 * Author		: adelwin
 * Create Date	: May 9, 2010 10:12:33 AM
 *
 */
package org.jampang.base.service.impl;

import java.util.List;

import org.jampang.base.context.LoginContext;
import org.jampang.base.exception.AuthenticationException;
import org.jampang.base.model.UserModel;
import org.jampang.base.service.IUserService;
import org.jampang.core.dao.IGenericHibernateDao;
import org.jampang.core.exception.GenericDaoException;
import org.jampang.core.exception.GenericServiceException;
import org.jampang.core.service.impl.BaseService;
import org.jampang.core.util.DateUtil;
import org.jampang.core.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class UserService extends BaseService implements IUserService {

	private IGenericHibernateDao<UserModel, String> userDao;
	private static Logger logger = LoggerFactory.getLogger(UserService.class);
	
	public IGenericHibernateDao<UserModel, String> getUserDao() {
		return userDao;
	}

	public void setUserDao(IGenericHibernateDao<UserModel, String> userDao) {
		this.userDao = userDao;
	}

	public LoginContext authenticateUser(UserModel inputModel) throws AuthenticationException {
		try {
			String userCode = inputModel.getUserCode();
			UserModel criterion = new UserModel();
			criterion.setUserCode(userCode);
			List<UserModel> userList = userDao.getListByCriteria(criterion, 1);
			if (userList.size() < 1) {
				throw new AuthenticationException("Invalid User Name or Password");
			} else {
				criterion = userList.get(0);
				LoginContext loginContext = new LoginContext();
				loginContext.setAttribute(LoginContext.KEY_USER_ID, criterion.getUserId());
				loginContext.setAttribute(LoginContext.KEY_USER_NAME, criterion.getUserCode());
				loginContext.setAttribute(LoginContext.KEY_USER_EMAIL, criterion.getEmail());
				loginContext.setAttribute(LoginContext.KEY_USER_RANK, criterion.getRank());
				loginContext.setAttribute(LoginContext.KEY_USER_REGION, criterion.getRegion());
				return loginContext;
			}
		} catch (GenericDaoException e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
			throw new AuthenticationException(e.getMessage(), e);
		}
	}

	public void lockUser(UserModel inputModel) throws GenericServiceException {
		try {
			inputModel.setStatus("L");
			userDao.save(inputModel);
		} catch (GenericDaoException e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
			throw new GenericServiceException(e.getMessage(), e);
		}
	}

	public LoginContext registerUser(UserModel inputModel, LoginContext loginContext) throws GenericServiceException {
		try {
			//check for duplicate user code, check by user code, the new user has no id yet
			UserModel criterion = new UserModel();
			criterion.setUserCode(inputModel.getUserCode());
			List<UserModel> userList = userDao.getListByCriteria(criterion, 1);
			if (userList.size() == 1) {
				//user code existed, throw
				throw new GenericServiceException("User Code is in use, choose a different one.");
			} else {
				//user code does not exist, insert
				if (StringUtil.isEmptyString(inputModel.getEmail())) {
					throw new GenericServiceException("User Data incomplete");
				}
				if (StringUtil.isEmptyString(inputModel.getRank())) {
					throw new GenericServiceException("User Data incomplete");
				}
				if (StringUtil.isEmptyString(inputModel.getRegion())) {
					throw new GenericServiceException("User Data incomplete");
				}
				if (StringUtil.isEmptyString(inputModel.getCreateBy())) {
					inputModel.setCreateBy(loginContext.getAttribute(LoginContext.KEY_USER_ID).toString());
				}
				if (inputModel.getCreateDate() == null) {
					inputModel.setCreateDate(DateUtil.today());
				}
				if (inputModel.getCreateBy() == null) {
					inputModel.setCreateBy((String) loginContext.getAttribute(LoginContext.KEY_USER_ID));
				}
				userDao.save(inputModel);
			}
			LoginContext newContext = new LoginContext();
			newContext.setAttribute(LoginContext.KEY_USER_ID, inputModel.getUserId());
			newContext.setAttribute(LoginContext.KEY_USER_NAME, inputModel.getUserCode());
			newContext.setAttribute(LoginContext.KEY_USER_EMAIL, inputModel.getEmail());
			newContext.setAttribute(LoginContext.KEY_USER_RANK, inputModel.getRank());
			newContext.setAttribute(LoginContext.KEY_USER_REGION, inputModel.getRegion());
			return newContext;
		} catch (GenericDaoException e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
			throw new GenericServiceException(e.getMessage(), e);
		}
	}


}
